namespace Study1.Common;

/// <summary>
/// 分页结果类
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
public class PagedResult<T>
{
    /// <summary>
    /// 数据列表
    /// </summary>
    public List<T> Data { get; set; } = new();
    
    /// <summary>
    /// 总记录数
    /// </summary>
    public int Total { get; set; }
    
    /// <summary>
    /// 当前页码
    /// </summary>
    public int PageIndex { get; set; }
    
    /// <summary>
    /// 每页大小
    /// </summary>
    public int PageSize { get; set; }
    
    /// <summary>
    /// 总页数
    /// </summary>
    public int TotalPages => (int)Math.Ceiling((double)Total / PageSize);
    
    /// <summary>
    /// 是否有上一页
    /// </summary>
    public bool HasPreviousPage => PageIndex > 1;
    
    /// <summary>
    /// 是否有下一页
    /// </summary>
    public bool HasNextPage => PageIndex < TotalPages;

    /// <summary>
    /// 构造函数
    /// </summary>
    public PagedResult() { }

    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="data">数据列表</param>
    /// <param name="total">总记录数</param>
    /// <param name="pageIndex">当前页码</param>
    /// <param name="pageSize">每页大小</param>
    public PagedResult(List<T> data, int total, int pageIndex, int pageSize)
    {
        Data = data;
        Total = total;
        PageIndex = pageIndex;
        PageSize = pageSize;
    }

    /// <summary>
    /// 创建分页结果
    /// </summary>
    /// <param name="data">数据列表</param>
    /// <param name="total">总记录数</param>
    /// <param name="pageIndex">当前页码</param>
    /// <param name="pageSize">每页大小</param>
    /// <returns></returns>
    public static PagedResult<T> Create(List<T> data, int total, int pageIndex, int pageSize)
    {
        return new PagedResult<T>(data, total, pageIndex, pageSize);
    }
}